import type { WeatherData } from "../types/weather";

const API_KEY = "61ef399be84b73ff95b472da83861129";

export async function getCurrentWeather(lat: number, lon: number) {
  try {
    const response = await fetch(
      `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${API_KEY}`
    );

    return await response.json();
  } catch (error: any) {
    throw new Error(error.message);
  }
}

export async function getForecastWeather(lat: number, lon: number) :Promise<WeatherData[]>{
  try {
    const response = await fetch(
      `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&units=metric&appid=${API_KEY}`
    );

    const data = await response.json();
    return data.list;
  } catch (error: any) {
    throw new Error(error.message);
  }
}
